package com.bobi.tfca.controller.message;

import com.bobi.tfca.model.entity.MemberEntity;
import com.bobi.tfca.model.pojo.R;
import com.bobi.tfca.model.vo.ChatRoom;
import com.bobi.tfca.model.vo.MessageVO;
import com.bobi.tfca.service.MessageService;
import com.bobi.tfca.web.annotation.CurrentUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李文发
 * @date 2024/4/8
 */
@RestController
@RequestMapping("/message")
@Api(tags = "消息接口")
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/findCRMessages/{groupId}")
    @ApiOperation(value = "查询历史聊天信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "聊天室Id"),
            @ApiImplicitParam(name = "member", value = "当前会员信息")
    })
    public R<List<MessageVO>> getCRHistoryMessages(@CurrentUser MemberEntity member, @PathVariable("groupId") Integer groupId) {
        List<MessageVO> res = messageService.getCRHistoryMessages(groupId, member);
        return R.data(res);
    }

    @ApiOperation("更新消息状态为已读")
    @PostMapping("/updateGroupMessageRead/{messageId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "messageId", value = "消息id"),
            @ApiImplicitParam(name = "member", value = "当前会员信息")
    })
    public R<String> updateMessageRead(@PathVariable("messageId") Long messageId, @CurrentUser MemberEntity member) {
        messageService.updateGroupMessageRead(messageId, member);
        return R.success();
    }

    @GetMapping("/checkNewMessage")
    @ApiOperation(value = "检查是否有新消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "member", value = "当前会员信息")
    })
    public R<String> checkNewMessage(@CurrentUser MemberEntity member) {
        Boolean success = messageService.checkNewMessage(member);
        return success ? R.success("有新消息") : R.failed("没有新消息");
    }
}